package com.oscar.web.service;

import com.alibaba.fastjson.JSON;
import com.oscar.web.dao.*;
import com.oscar.web.domain.*;
import com.oscar.web.util.LogData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

@Service
public class RecommenderService {
    //日志采集器
    Logger logger= LoggerFactory.getLogger(BookService.class);

    @Autowired
    private RateMoreRecentlyBooksRepository rateMoreRecentlyBooksRepository;

    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private StreamrecsRepository streamrecsRepository;

    @Autowired
    private AverageBooksScoreRepository averageBooksScoreRepository;

    @Autowired
    private RateMoreBooksRepository rateMoreBooksRepository;

    @Autowired
    private ItemCFBookrecsRepository itemCFBookrecsRepository;

    //获取近期热门推荐
    public List<Book> getHotRecommendations(Integer pageNumber){
        List<Book> list = new ArrayList<>();//新建一个列表就存放图书
        //获取一页书的信息
        Page<RateMoreRecentlyBooks> page = rateMoreRecentlyBooksRepository.findByOrderByYearmonthDescCountDesc(PageRequest.of(pageNumber, 20));
        Iterator<RateMoreRecentlyBooks> iterator = page.iterator() ;
        while (iterator.hasNext()){
            RateMoreRecentlyBooks b = iterator.next();
            Book book = bookRepository.findBookByBookId(b.getBookid());
            System.out.println("==获取近期热门推荐===========>>"+book.getTitle()+book.getBookId());
            list.add(book);
        }
        //将页面信息通过JSON.toJSON(logData).toString() 方法封装成字符串
        LogData<Iterator<RateMoreRecentlyBooks>> logData = new LogData("search" , page.iterator()) ;
        String log = JSON.toJSON(logData).toString() ;
        //将信息存到日志中
        logger.info("埋点日志===========>"+log);
        return  list ;
    }

    //获取历史热门推荐
    public List<Book> getHistoryHotRecommendations(Integer pageNumber){
        List<Book> list = new ArrayList<>();//新建一个列表就存放图书
        //获取一页书的信息
        Page<RateMoreBooks> page = rateMoreBooksRepository.findByOrderByCountDesc(PageRequest.of(pageNumber, 20));
        Iterator<RateMoreBooks> iterator = page.iterator() ;
        while (iterator.hasNext()){
            RateMoreBooks b = iterator.next();
            Book book = bookRepository.findBookByBookId(b.getBookid());
            list.add(book);
        }
        //将页面信息通过JSON.toJSON(logData).toString() 方法封装成字符串
        LogData<Iterator<RateMoreRecentlyBooks>> logData = new LogData("search" , page.iterator()) ;
        String log = JSON.toJSON(logData).toString() ;
        //将信息存到日志中
        logger.info("埋点日志===========>"+log);
        return  list ;
    }
    public Page<RateMoreBooks> getHistoryHot(Integer pageNumber) {
        Page<RateMoreBooks> page = rateMoreBooksRepository.findByOrderByCountDesc(PageRequest.of(pageNumber, 20));
        return page;
    }

    //获取实时推荐
    public List<Book> getStreamRecommendations(int userid) {
        List<Book> list = new ArrayList<>();//新建一个列表就存放图书
        Streamrecs s = streamrecsRepository.findByUserid(userid);//获取实时推荐数据Streamrecs（int bookid,String recs）
        //recs 格式：|2067534:2.5903993541115113|9948685:2.4198163071654726
        String[] recs = s.getRecs().substring(1).split("\\|");//这里要把第一个字符串删除，不然之后会出现空字符串，不能转为int
        for (String a:recs) {
            String bookid=a.split("\\:")[0];
            Book book = bookRepository.findBookByBookId(Integer.valueOf(bookid).intValue());//bookid转为int作为键查找book
            list.add(book);
        }
        return list;
    }

    //获取优质图书推荐
    public List<Book> getGoodRecommendations(int pageNumber) {
        List<Book> list = new ArrayList<>();//新建一个列表就存放图书
        Page<AverageBooksScore> page=averageBooksScoreRepository.findByOrderByAvgDesc(PageRequest.of(0, 20));
        Iterator<AverageBooksScore> iterator = page.iterator() ;
        while (iterator.hasNext()){
            AverageBooksScore b = iterator.next();
            Book book = bookRepository.findBookByBookId(b.getBookid());
            list.add(book);
        }
        return list;
    }

    //获取相似图书推荐
    public List<Book> ItemCFRecommendations(int bookid) {
        List<Book> list = new ArrayList<>();//新建一个列表就存放图书
        List<Integer> list2 = new ArrayList<>();
        ItemCFBookrecs book = itemCFBookrecsRepository.findByBookid(bookid);
        String recs=book.getRecs();
        String[] aa =  recs.split("\\|");
        for (String a:aa) {
            String c=a.split("\\:")[0];
            int cc=Integer.valueOf(c).intValue();
            System.out.println("====>"+cc);
            Book book1 = bookRepository.findBookByBookId(cc);
            list.add(book1);
        }
//        Iterator<ItemCFBookrecs> iterator = page.iterator() ;
//        while (iterator.hasNext()){
//            ItemCFBookrecs b = iterator.next();
//            Book book = bookRepository.findBookByBookId(b.getBookid());
//            list.add(book);
//        }
        return list;
    }


}
